/**************************************************************************
 *  创建者：        hualei
 *  开始时间：   2018.06.23
 *  copyright:  (C) 深圳市华友高科有限公司
 *  修改说明：  (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 *
 ***************************************************************************/
#include "tcpreadinstructionview.h"
#include "ui_tcpreadinstructionview.h"
#include "../../programView/programview.h"

TCPReadInstructionView::TCPReadInstructionView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TCPReadInstructionView)
{
    ui->setupUi(this);
    initialDisplayStyle(styleId);

    tcpReadClientIPVec.resize(4);
    tcpReadClientIPVec[0] = 127;
    tcpReadClientIPVec[1] = 0;
    tcpReadClientIPVec[2] = 0;
    tcpReadClientIPVec[3] = 1;

    tcpReadServerIPVec.resize(4);
    tcpReadServerIPVec[0] = 192;
    tcpReadServerIPVec[1] = 168;
    tcpReadServerIPVec[2] = 111;
    tcpReadServerIPVec[3] = 36;

    ip1 = 0;
    ip2 = 0;
    ip3 = 0;
    ip4 = 0;

    ui->RegLength_Edit->setValidator(new QIntValidator(0,10000,this));
    ui->PORT_Edit->setValidator(new QIntValidator(0,65536,this));

    ui->lineEdit_ip1->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip2->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip3->setValidator(new QIntValidator(0,255,this));
    ui->lineEdit_ip4->setValidator(new QIntValidator(0,255,this));

}

void TCPReadInstructionView::initialDisplayStyle(int styleId)
{
    switch (styleId) {
    case 1:
        ui->note_GBox_2->hide();
        ui->forward_Btn->setGeometry(10,350,70,40);
        ui->save_Btn->setGeometry(360,350,70,40);
        break;
    default:
        break;
    }
}

void TCPReadInstructionView::getInput()
{
    tcpReadPort = ui->PORT_Edit->text().toInt();
    tcpReadRegBeginAddress = ui->RegBeginAddress_Edit->text().toInt();
    tcpReadRegLength = ui->RegLength_Edit->text().toInt();

    if(tcpReadBusType == E_TCP_READ_BUS_CLIENT)
    {
        tcpReadServerIPVec[0] = ui->lineEdit_ip1->text().toInt();
        tcpReadServerIPVec[1] = ui->lineEdit_ip2->text().toInt();
        tcpReadServerIPVec[2] = ui->lineEdit_ip3->text().toInt();
        tcpReadServerIPVec[3] = ui->lineEdit_ip4->text().toInt();
    }
    else if(tcpReadBusType == E_TCP_READ_BUS_SERVER)
    {
        tcpReadClientIPVec[0] = ui->lineEdit_ip1->text().toInt();
        tcpReadClientIPVec[1] = ui->lineEdit_ip2->text().toInt();
        tcpReadClientIPVec[2] = ui->lineEdit_ip3->text().toInt();
        tcpReadClientIPVec[3] = ui->lineEdit_ip4->text().toInt();
    }

    jmpLBValue = ui->JMP_LB_SpinBox->text().toInt();
    noteStr = ui->note_lineEdit->text();

}

void TCPReadInstructionView::updateView()
{
    ui->CLIENT_radioButton->setChecked(false);
    ui->SERVER_radioButton->setChecked(false);

    if(tcpReadBusType == E_TCP_READ_BUS_CLIENT)
    {
        ui->CLIENT_radioButton->setChecked(true);
        ui->IP_Label->setText("Server IP:");
        ui->lineEdit_ip1->setText(QString::number(tcpReadServerIPVec[0]));
        ui->lineEdit_ip2->setText(QString::number(tcpReadServerIPVec[1]));
        ui->lineEdit_ip3->setText(QString::number(tcpReadServerIPVec[2]));
        ui->lineEdit_ip4->setText(QString::number(tcpReadServerIPVec[3]));
    }
    else if(tcpReadBusType == E_TCP_READ_BUS_SERVER)
    {
        ui->SERVER_radioButton->setChecked(true);
        ui->IP_Label->setText("Client IP:");
        ui->lineEdit_ip1->setText(QString::number(tcpReadClientIPVec[0]));
        ui->lineEdit_ip2->setText(QString::number(tcpReadClientIPVec[1]));
        ui->lineEdit_ip3->setText(QString::number(tcpReadClientIPVec[2]));
        ui->lineEdit_ip4->setText(QString::number(tcpReadClientIPVec[3]));
    }

    ui->RegBeginAddress_Edit->setText(QString::number(tcpReadRegBeginAddress));
    ui->RegLength_Edit->setText(QString::number(tcpReadRegLength));
    ui->PORT_Edit->setText(QString::number(tcpReadPort));
    ui->JMP_LB_SpinBox->setValue(jmpLBValue);
    ui->note_lineEdit->setText(noteStr);
}

void TCPReadInstructionView::changLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/TCPReadInstructionViewEnglish.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);
}


void TCPReadInstructionView::on_clearNote_Btn_clicked()
{
    ui->note_lineEdit->clear();
    emit cleanNoteStrSignal();
}

void TCPReadInstructionView::on_save_Btn_clicked()
{
    getInput();
    emit finishEditInstructionSignal();
    emit saveInstruction(instructionOut);
}

void TCPReadInstructionView::on_forward_Btn_clicked()
{

    this->close();
    programView->instructionListFlag = 0;
    programView->editInstructionViewShowOrHide();

}

void TCPReadInstructionView::on_CLIENT_radioButton_clicked()
{
    tcpReadBusType = E_TCP_READ_BUS_CLIENT;

    ui->lineEdit_ip1->setText(QString::number(ip1));
    ui->lineEdit_ip2->setText(QString::number(ip2));
    ui->lineEdit_ip3->setText(QString::number(ip3));
    ui->lineEdit_ip4->setText(QString::number(ip4));

    ui->IP_Label->setText("Server IP:");
}

void TCPReadInstructionView::on_SERVER_radioButton_clicked()
{
    ip1 = ui->lineEdit_ip1->text().toInt();
    ip2 = ui->lineEdit_ip2->text().toInt();
    ip3 = ui->lineEdit_ip3->text().toInt();
    ip4 = ui->lineEdit_ip4->text().toInt();

    tcpReadBusType = E_TCP_READ_BUS_SERVER;
    ui->IP_Label->setText("Client IP:");
    ui->lineEdit_ip1->setText("127");
    ui->lineEdit_ip2->setText("0");
    ui->lineEdit_ip3->setText("0");
    ui->lineEdit_ip4->setText("1");
}
